<?
include ("config.php");
$conn=mysql_connect($host,$user,$password);
mysql_select_db($db);
mysql_query("SET NAMES UTF8");
$ip=$_SERVER['REMOTE_ADDR'];
$arr_ip=explode(".",$ip);
$str_ip="";
for($i=0;$i<count($arr_ip);$i++)
{
	$str_ip.=$arr_ip[$i];
}
$sql_ip="select * from vote_ip where ip='$str_ip'";
$query_ip=mysql_query($sql_ip);
/*
if(mysql_num_rows($query_ip))
{
	echo "<script language='javascript'>alert('One vote per one IP address!');window.close();</script>";
	exit();
}
else
{
 */
	foreach($_POST as $key=>$value)
	{
		$arr=explode("_",$key);
		if(!is_array($arr)) 
			continue;
		$titleid=$arr[1];
		$id=$value;
		$result=mysql_query("select * from vote_title where id='$titleid'");
		$row=mysql_fetch_assoc($result);
		if($row['options']=='a')
		{
			$query="update vote_choice set num=num+1 where id='$id'";
			$result1=mysql_query($query);
		} 
		else if($row['options']=='b')
		{
			for($i=0;$i<count($_POST[$key]);$i++)
			{   
				$query="update vote_choice set num=num+1 where id=".$_POST[$key][$i];
				$result=mysql_query($query);
			}
		}
	}
	mysql_query("insert into vote_ip(ip) values ('$str_ip')");
	echo "<script language='javascript'>alert('Vote finished successfully!');window.close();</script>";
/*
}
 */
?>
